package o;

import android.app.Activity;
import com.huawei.appgallery.foundation.account.bean.UserSession;
import com.huawei.hms.api.ConnectionResult;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hms.api.HuaweiApiClient;
import com.huawei.hms.api.HuaweiApiClient.Builder;
import com.huawei.hms.api.HuaweiApiClient.ConnectionCallbacks;
import com.huawei.hms.api.HuaweiApiClient.OnConnectionFailedListener;
import com.huawei.hms.support.api.client.Result;
import com.huawei.hms.support.api.client.ResultCallback;
import com.huawei.hms.support.api.client.Status;
import com.huawei.hms.support.api.entity.core.CommonCode.ErrorCode;
import com.huawei.hms.support.api.hwid.HuaweiId;
import com.huawei.hms.support.api.hwid.HuaweiIdSignInOptions;
import com.huawei.hms.support.api.hwid.SignInResult;
import com.huawei.hms.support.api.sns.HuaweiSns;
import com.huawei.hms.support.api.sns.IntentResult;
import com.huawei.hms.support.api.sns.UnreadMsgCountResult;
import com.huawei.hms.support.api.sns.UserUnreadMsgCountResult;
import com.huawei.updatesdk.service.otaupdate.UpdateStatusCode.DialogButton;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import o.dky.j;

public class eku implements ConnectionCallbacks, OnConnectionFailedListener {
    private static eku ˎ;
    private WeakReference<Activity> ʻ;
    private b ʼ;
    private HuaweiApiClient ʽ;
    private final List<b> ˊ = new ArrayList();
    private final List<b> ˋ = new ArrayList();
    private boolean ˏ = false;
    private final List<b> ॱ = new ArrayList();
    private boolean ॱॱ = false;
    private boolean ᐝ = false;

    interface b {
        void ˎ();
    }

    static class a implements ResultCallback<UnreadMsgCountResult> {
        private WeakReference<ekn> ˋ;

        public /* synthetic */ void onResult(Object obj) {
            ˎ((UnreadMsgCountResult) obj);
        }

        a(WeakReference<ekn> weakReference) {
            this.ˋ = weakReference;
        }

        public void ˎ(UnreadMsgCountResult unreadMsgCountResult) {
            if (unreadMsgCountResult.getStatus() == Status.SUCCESS) {
                Logger.ˏ("SnsAgent", "unreadMsgCountResult isSuccess.");
                if (this.ˋ != null) {
                    ekn o_ekn = (ekn) this.ˋ.get();
                    if (o_ekn != null) {
                        o_ekn.ˏ(unreadMsgCountResult);
                        return;
                    }
                    return;
                }
                Logger.e("SnsAgent", "handler is null");
                return;
            }
            Logger.e("SnsAgent", "getUnreadMsgCount failed, status:" + unreadMsgCountResult.getStatus());
        }
    }

    static class c implements AccountBussinessListener {
        private c() {
        }

        public void onAccountBusinessResult(ccp o_ccp) {
            eoj.ˋ().ˊ("showUI");
            if (DialogButton.CONFIRM == o_ccp.ˎ) {
                eqq.ˋ().ˊ("showUI");
            }
        }
    }

    static class d implements ResultCallback<IntentResult> {
        private final Activity ॱ;

        public /* synthetic */ void onResult(Object obj) {
            ॱ((IntentResult) obj);
        }

        d(Activity activity) {
            this.ॱ = activity;
        }

        public void ॱ(IntentResult intentResult) {
            if (intentResult == null || intentResult.getStatus() == null) {
                Logger.e("SnsAgent", "result or result.getStatus() null");
            } else if (intentResult.getStatus() == Status.SUCCESS) {
                this.ॱ.startActivity(intentResult.getIntent());
            } else {
                if (ErrorCode.NOT_IN_SERVICE == intentResult.getStatus().getStatusCode()) {
                    evk.ॱ(ContentManager.getInstant().getContext().getString(j.ʽˋ), 0).ˋ();
                } else {
                    evk.ॱ(ContentManager.getInstant().getContext().getString(j.ॱॱ), 0).ˋ();
                }
                Logger.e("SnsAgent", "showUI failed, status:" + intentResult.getStatus());
            }
        }
    }

    static class e implements eqn {
        private int ˊ;
        private WeakReference<Activity> ˋ;
        private long ˎ;

        private e(int i, long j, Activity activity) {
            this.ˊ = i;
            this.ˎ = j;
            this.ˋ = new WeakReference(activity);
        }

        public void onResult(int i) {
            eqq.ˋ().ˊ("showUI");
            Activity activity = (Activity) this.ˋ.get();
            if (activity != null && 202 == i) {
                eku.ˏ().ˋ(this.ˊ, this.ˎ, activity);
            }
        }
    }

    static class g implements ResultCallback<UserUnreadMsgCountResult> {
        private final WeakReference<ekn> ˏ;

        public /* synthetic */ void onResult(Object obj) {
            ˎ((UserUnreadMsgCountResult) obj);
        }

        g(WeakReference<ekn> weakReference) {
            this.ˏ = weakReference;
        }

        public void ˎ(UserUnreadMsgCountResult userUnreadMsgCountResult) {
            if (userUnreadMsgCountResult.getStatus() != Status.SUCCESS) {
                Logger.e("SnsAgent", "queryUserUnreadMsgCount failed, status:" + userUnreadMsgCountResult.getStatus());
            }
            if (this.ˏ != null) {
                ekn o_ekn = (ekn) this.ˏ.get();
                if (o_ekn != null) {
                    o_ekn.ˏ(userUnreadMsgCountResult);
                    return;
                } else {
                    Logger.e("SnsAgent", "eventHandler is null");
                    return;
                }
            }
            Logger.e("SnsAgent", "handler is null");
        }
    }

    public static synchronized eku ˏ() {
        eku o_eku;
        synchronized (eku.class) {
            if (ˎ == null) {
                ˎ = new eku();
            }
            o_eku = ˎ;
        }
        return o_eku;
    }

    public void ˊ(final ekn o_ekn) {
        if (UserSession.getInstance().isLoginSuccessful()) {
            try {
                if (this.ʽ == null || !this.ʽ.isConnected()) {
                    this.ˊ.add(new b(this) {
                        final /* synthetic */ eku ˋ;

                        public void ˎ() {
                            this.ˋ.ˊ(o_ekn);
                        }
                    });
                    if (this.ʻ != null) {
                        ॱ((Activity) this.ʻ.get(), false);
                        return;
                    } else {
                        ॱ(null, false);
                        return;
                    }
                } else if (this.ᐝ) {
                    HuaweiSns.HuaweiSnsApi.getUnreadMsgCount(this.ʽ).setResultCallback(new a(new WeakReference(o_ekn)));
                    return;
                } else {
                    ˊ(new b(this) {
                        final /* synthetic */ eku ˎ;

                        public void ˎ() {
                            this.ˎ.ˊ(o_ekn);
                        }
                    });
                    return;
                }
            } catch (Exception e) {
                Logger.e("SnsAgent", "UnreadFriendMsgResultCallback Exception");
                return;
            }
        }
        Logger.ˏ("SnsAgent", "queryUnreadMsgCount, not login");
    }

    public void ॱ(final ekn o_ekn) {
        if (!UserSession.getInstance().isLoginSuccessful() || ((eov) cdb.ॱ(eov.class)).ॱ(ContentManager.getInstant().getContext())) {
            if (Logger.ˎ()) {
                Logger.ˏ("SnsAgent", "queryUserUnreadMsgCount, not login");
            }
            if (o_ekn != null) {
                Result userUnreadMsgCountResult = new UserUnreadMsgCountResult();
                userUnreadMsgCountResult.setCount(0);
                o_ekn.ˏ(userUnreadMsgCountResult);
                return;
            }
            return;
        }
        try {
            if (this.ʽ == null || !this.ʽ.isConnected()) {
                this.ˊ.add(new b(this) {
                    final /* synthetic */ eku ˊ;

                    public void ˎ() {
                        this.ˊ.ॱ(o_ekn);
                    }
                });
                this.ˋ.add(new b(this) {
                    final /* synthetic */ eku ॱ;

                    public void ˎ() {
                        Result userUnreadMsgCountResult = new UserUnreadMsgCountResult();
                        userUnreadMsgCountResult.setCount(0);
                        o_ekn.ˏ(userUnreadMsgCountResult);
                        this.ॱ.ˋ.clear();
                    }
                });
                if (this.ʻ != null) {
                    ॱ((Activity) this.ʻ.get(), false);
                } else {
                    ॱ(null, false);
                }
            } else if (this.ᐝ) {
                HuaweiSns.HuaweiSnsApi.getUserCount(this.ʽ, ॱ()).setResultCallback(new g(new WeakReference(o_ekn)));
            } else {
                ˊ(new b(this) {
                    final /* synthetic */ eku ॱ;

                    public void ˎ() {
                        this.ॱ.ॱ(o_ekn);
                    }
                });
            }
        } catch (Exception e) {
            Logger.e("SnsAgent", "queryUserUnreadMsgCount Exception");
        }
    }

    public void ॱ(Activity activity) {
        ˋ(7, ॱ(), activity);
    }

    public synchronized void ॱ(Activity activity, boolean z) {
        if (((eov) cdb.ॱ(eov.class)).ॱ(ContentManager.getInstant().getContext())) {
            Logger.ˎ("SnsAgent", "Sorry, need interrupt sns init.");
        } else {
            Logger.ˏ("SnsAgent", "init initSucc:" + this.ˏ);
            if (!this.ˏ) {
                try {
                    this.ʻ = new WeakReference(activity);
                    this.ॱॱ = z;
                    if (this.ʽ == null) {
                        this.ʽ = new Builder(ContentManager.getInstant().getContext()).addApi(HuaweiId.SIGN_IN_API, new HuaweiIdSignInOptions.Builder(HuaweiIdSignInOptions.DEFAULT_SIGN_IN).requestUid().build()).addApi(HuaweiSns.API).addScope(HuaweiId.HUAEWEIID_BASE_SCOPE).addScope(HuaweiSns.SCOPE_SNS_READ).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
                    }
                    this.ʽ.connect(null);
                } catch (Exception e) {
                    Logger.e("SnsAgent", "init error " + e.getMessage());
                }
            }
        }
    }

    private void ˊ(b bVar) {
        Logger.ˏ("SnsAgent", "signIn.");
        this.ॱ.add(bVar);
        if (this.ᐝ) {
            ˋ(this.ॱ);
        } else {
            HuaweiId.HuaweiIdApi.signInBackend(this.ʽ).setResultCallback(new ResultCallback<SignInResult>(this) {
                final /* synthetic */ eku ˊ;

                {
                    this.ˊ = r1;
                }

                public /* synthetic */ void onResult(Object obj) {
                    ˎ((SignInResult) obj);
                }

                public void ˎ(SignInResult signInResult) {
                    if (signInResult == null) {
                        Logger.e("SnsAgent", "signIn result == null");
                        this.ˊ.ॱ.clear();
                    } else if (signInResult.isSuccess()) {
                        Logger.ˏ("SnsAgent", "signIn success");
                        this.ˊ.ᐝ = true;
                        this.ˊ.ˋ(this.ˊ.ॱ);
                    } else {
                        Logger.e("SnsAgent", "signIn failed, status:" + signInResult.getStatus());
                        this.ˊ.ॱ.clear();
                    }
                }
            });
        }
    }

    private void ˋ(int i, long j, Activity activity) {
        if (UserSession.getInstance().isLoginSuccessful()) {
            try {
                if (this.ʽ == null || !this.ʽ.isConnected()) {
                    final int i2 = i;
                    final long j2 = j;
                    final Activity activity2 = activity;
                    this.ʼ = new b(this) {
                        final /* synthetic */ eku ˋ;

                        public void ˎ() {
                            this.ˋ.ˋ(i2, j2, activity2);
                        }
                    };
                    ॱ(activity, true);
                    return;
                }
                HuaweiSns.HuaweiSnsApi.getUiIntent(this.ʽ, i, j).setResultCallback(new d(activity));
                return;
            } catch (Throwable e) {
                Logger.ˋ("SnsAgent", "showUI", e);
                return;
            }
        }
        eoj.ˋ().ˎ("showUI", new c());
        eqq.ˋ().ˎ("showUI", new e(i, j, activity));
        eod.ˊ(activity, null, false, true, true);
    }

    public synchronized void ˊ() {
        if (this.ˏ) {
            try {
                if (this.ʽ != null) {
                    this.ʽ.disconnect();
                }
                this.ˏ = false;
                this.ᐝ = false;
                this.ʻ = null;
                this.ˊ.clear();
                this.ॱ.clear();
                this.ˋ.clear();
                this.ʼ = null;
                Logger.ˏ("SnsAgent", "destroy succ");
            } catch (Throwable e) {
                Logger.ˋ("SnsAgent", "destroy error", e);
            }
        }
    }

    private void ˋ(List<b> list) {
        if (CollectCheck.isEmpty(list)) {
            Logger.e("SnsAgent", "list is empty");
            return;
        }
        ArrayList arrayList = new ArrayList();
        arrayList.addAll(list);
        list.clear();
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            b bVar = (b) it.next();
            if (bVar != null) {
                bVar.ˎ();
            }
            it.remove();
        }
    }

    public void onConnected() {
        Logger.ˏ("SnsAgent", "onConnected");
        if (this.ʽ != null && this.ʽ.isConnected()) {
            this.ˏ = true;
            this.ᐝ = false;
            if (this.ʼ != null) {
                this.ˊ.add(this.ʼ);
            }
            ˋ(this.ˊ);
        }
    }

    public void onConnectionSuspended(int i) {
        Logger.ˏ("SnsAgent", "onConnectionSuspended, cause: " + i);
        this.ˏ = false;
        this.ˊ.clear();
        this.ॱ.clear();
        this.ˋ.clear();
        this.ʼ = null;
        if (this.ॱॱ) {
            evk.ॱ(ContentManager.getInstant().getContext().getString(j.ॱॱ), 0).ˋ();
        }
    }

    public void onConnectionFailed(ConnectionResult connectionResult) {
        int errorCode = connectionResult.getErrorCode();
        Logger.ˏ("SnsAgent", "onConnectionFailed, ErrorCode: " + errorCode);
        if (!this.ˏ) {
            if (this.ॱॱ) {
                HuaweiApiAvailability instance = HuaweiApiAvailability.getInstance();
                if (instance.isUserResolvableError(errorCode) && this.ʻ != null) {
                    Activity activity = (Activity) this.ʻ.get();
                    if (!(activity == null || activity.isFinishing())) {
                        instance.resolveError(activity, errorCode, 1001);
                    }
                }
                if (2 != errorCode) {
                    evk.ॱ(ContentManager.getInstant().getContext().getString(j.ॱॱ), 0).ˋ();
                }
            }
            this.ˊ.clear();
            this.ॱ.clear();
            ˋ(this.ˋ);
            this.ʼ = null;
        }
    }

    private long ॱ() {
        return duv.ˋ().ˏ().ˋ();
    }
}
